package com.ruibo.modules.system.domain;

import com.ruibo.jpa.model.AbstractAuditModel;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * @CLassName User
 * @Description TODO
 * @Author 孙峰
 * @Date 2020/1/1 17:56
 * @Version 1.0
 */
@Entity
@Getter
@Setter
@Table(name="sys_user")
public class SysUser extends AbstractAuditModel implements Serializable {

    private static final long serialVersionUID = 1L;



    /**
     * 用户账号
     */
    @Column(name = "user_name",length = 30,nullable = false)
    private String userName;

    /**
     * 用户昵称
     */
    @Column(name = "nick_name",length = 30, nullable = false)
    private String nickName;

    /**
     * 用户类型（00系统用户）
     */
    @Column(name = "user_type",length = 2)
    private String useType;

    /**
     * 用户邮箱
     */
    @Column(name = "email",length = 50)
    private String email;

    /**
     * 手机号码
     */
    @Column(name = "phone_number",length = 11)
    private String phoneNumber;

    /**
     * 用户性别
     */
    @Column(name = "sex",length = 1)
    private String sex;

    /**
     * 用户头像
     */
    @Column(name = "avatar",length = 100)
    private String avatar;

    /**
     * 密码
     */
    @Column(name = "password",length = 100)
    private String password;



    /**
     * 帐号状态（1正常 0停用）
     */
    @Column(name = "status",length = 1)
    private String status;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    @Column(name = "del_flag",length = 1)
    private String delFlag;

    /**
     * 最后登陆IP
     */
    @Column(name = "login_ip",length = 50)
    private String loginIp;

    /**
     * 最后登陆时间
     */
    @Column(name = "login_date")
    private LocalDateTime loginDate;

    // 部门
    @OneToOne
    @JoinColumn(name = "dept_id")
    private SysDept dept;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "sys_users_roles", joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")})
    private Set<SysRole> sysRoles;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "sys_user_post", joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "post_id",referencedColumnName = "id")})
    private Set<SysPost> sysPost;

    public boolean isAdmin() {
        return isAdmin(getId());
    }
    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }


    public @interface Update {}


}
